/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Distinctive;
import com.click.app.model.DistinctiveName;
import com.click.app.model.DistinctiveValue;
import com.click.app.model.Material;
import com.click.app.service.DistinctiveManager;
import com.click.app.service.DistinctiveNameManager;
import com.click.app.service.DistinctiveValueManager;
import com.click.app.service.GenericManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author orgale
 */
public class DistinctiveAction extends BaseAction {

    private DistinctiveManager distinctiveManager;
    private GenericManager<Material, Long> materialManager;
    private DistinctiveNameManager distinctiveNameManager;
    private DistinctiveValueManager distinctiveValueManager;
    private List distinctives;
    private Distinctive distinctive;
    private Long id;
    private String name;

    public void setId(Long id) {
        this.id = id;
    }

    public Distinctive getDistinctive() {
        return distinctive;
    }

    public void setDistinctive(Distinctive distinctive) {
        this.distinctive = distinctive;
    }

    public boolean materialsFree(Distinctive dis) {
        boolean resp = true;

        List<Material> mats = materialManager.getAll();
        for (Material m : mats) {
            for (Distinctive d : m.getDistinctives()) {
                if (d.equals(dis)) {
                    resp = false;

                }
            }
        }

        return resp;
    }

    public String delete() {
        if (materialsFree(distinctive)) {
            distinctiveManager.remove(new Long(distinctive.getId()));
            saveMessage(getText("distinctive.deleted"));
        } else {
            List messages = (List) getRequest().getSession().getAttribute("errors");
            if (messages == null) {
                messages = new ArrayList();
            }
            messages.add(getText("distinctive.deleteError"));
            getRequest().getSession().setAttribute("errors", messages);

        }
        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            distinctive = distinctiveManager.get(id);
        } else {
            distinctive = new Distinctive();
        }

        return SUCCESS;
    }

    public Distinctive getDistinctiveWithNameAndValue(String n, String v) {
        Distinctive d = null;
        for (Distinctive di : distinctiveManager.getAll()) {
            if ((di.getValue().matches(v)) && (di.getName().matches(n))) {
                d = di;
            }
        }
        return d;
    }

    public boolean validationPasses() {
        boolean vp = true;
        if (distinctive.getName() == null || distinctive.getName().matches("")) {
            errorMessage("Campo requerido, nombre.");
            vp = false;
        }
        if (distinctive.getValue() == null || distinctive.getValue().matches("")) {
            errorMessage("Campo requerido, valor.");
            vp = false;
        }
        System.out.println("val:"+vp);
        return vp;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (distinctive.getId() == null);

        DistinctiveName dn = new DistinctiveName();
        DistinctiveValue dv = new DistinctiveValue();
        if ((getDistinctiveWithNameAndValue(distinctive.getName(), distinctive.getValue())) != null) {
            errorMessage("El distintivo ya existe.");
            System.out.println("Distinctive already exists...");

        } else {
            boolean exists = false;
            for (DistinctiveName d : distinctiveNameManager.getAll()) {
                if (distinctive.getName().matches(d.getName())) {
                    dn = d;
                    exists = true;
                }
            }

            //if distinctivename exists
            if (exists) {
            } else {
                dn.setName(distinctive.getName());
                dn = distinctiveNameManager.save(dn);
            }

            //is distinctive is new
            dv.setName(distinctive.getValue());
            dv.setDistinctiveName(dn);
            dv = distinctiveValueManager.save(dv);
            dn.getDistinctiveValues().add(dv);

            //DistinctiveValue dn = new DistinctiveName();
            //dn.setName(distinctive.getName());
            if (validationPasses()) {
                distinctive = distinctiveManager.save(distinctive);
            } else {
                return ERROR;
            }
            String key = (isNew) ? "distinctive.added" : "distinctive.updated";
            saveMessage(getText(key));
        }
        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setDistinctiveManager(DistinctiveManager distinctiveManager) {
        this.distinctiveManager = distinctiveManager;
    }

    public void setDistinctiveNameManager(DistinctiveNameManager distinctiveNameManager) {
        this.distinctiveNameManager = distinctiveNameManager;
    }

    public void setDistinctiveValueManager(DistinctiveValueManager distinctiveValueManager) {
        this.distinctiveValueManager = distinctiveValueManager;
    }

    public void setMaterialManager(GenericManager<Material, Long> materialManager) {
        this.materialManager = materialManager;
    }

    public List getDistinctives() {
        return distinctives;
    }

    public String list() {
        distinctives = distinctiveManager.getAll();
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    public String help() {
        return SUCCESS;
    }

    public List<DistinctiveName> getDNames() {
        List<DistinctiveName> dNames = new ArrayList<DistinctiveName>();
        return distinctiveNameManager.getAll();
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String saveDistinctiveValue() {
        System.out.println("NAME: " + name);
        System.out.println("id: " + getRequest().getAttribute("id"));
        System.out.println("d: " + getRequest().getAttribute("distinctive"));

        return SUCCESS;
    }
    private String distinctiveName;
    private String distinctiveValue;

    public String deleteDistinctiveValue() {
        if (materialsFree(distinctiveManager.findDistinctivesByNameAndValue(getDistinctiveName(), getDistinctiveValue()).get(0))) {
            distinctiveManager.remove((distinctiveManager.findDistinctivesByNameAndValue(getDistinctiveName(), getDistinctiveValue())).get(0).getId());
            distinctiveValueManager.remove(distinctiveValueManager.findDistinctiveValuesByName(getDistinctiveValue()).getId());
            DistinctiveName dn = distinctiveNameManager.findDistinctiveNamesByName(getDistinctiveName());
            if (dn.getDistinctiveValues().isEmpty()) {
                distinctiveNameManager.remove(distinctiveNameManager.findDistinctiveNamesByName(getDistinctiveName()).getId());
            }
            saveMessage(getText("distinctive.deleted"));
        } else {
            List messages = (List) getRequest().getSession().getAttribute("errors");
            if (messages == null) {
                messages = new ArrayList();
            }
            messages.add(getText("distinctive.deleteError"));
            getRequest().getSession().setAttribute("errors", messages);

        }
        return SUCCESS;
    }

    public List<DistinctiveName> getDistinctiveNames() {
        return distinctiveNameManager.getAll();
    }

    /**
     * @return the distinctiveName
     */
    public String getDistinctiveName() {
        return distinctiveName;
    }

    /**
     * @param distinctiveName the distinctiveName to set
     */
    public void setDistinctiveName(String distinctiveName) {
        this.distinctiveName = distinctiveName;
    }

    /**
     * @return the distinctiveValue
     */
    public String getDistinctiveValue() {
        return distinctiveValue;
    }

    /**
     * @param distinctiveValue the distinctiveValue to set
     */
    public void setDistinctiveValue(String distinctiveValue) {
        this.distinctiveValue = distinctiveValue;
    }
}